package com.study.spring.disruptor;

import com.lmax.disruptor.EventTranslator;
import com.lmax.disruptor.dsl.Disruptor;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 功能描述：
 * <p>
 * author: ZACK
 * date: 2021/3/10 23:23
 */
public class TradeTransactionPublisher implements Runnable {

    private static final int LOOP = 10;//模拟一千万次交易的发生
    Disruptor<TradeTransaction> disruptor;
    private final CountDownLatch latch;

    public TradeTransactionPublisher(CountDownLatch latch, Disruptor<TradeTransaction> disruptor) {
        this.disruptor = disruptor;
        this.latch = latch;
    }

    @Override
    public void run() {
        TradeTransactionEventTranslator tradeTransloator = new TradeTransactionEventTranslator();
        for (int i = 0; i < LOOP; i++) {
            disruptor.publishEvent(tradeTransloator);
        }
        latch.countDown();
    }

    class TradeTransactionEventTranslator implements EventTranslator<TradeTransaction> {

        private final Random random = new Random();

        @Override
        public void translateTo(TradeTransaction event, long sequence) {
            this.generateTradeTransaction(event);
        }

        private TradeTransaction generateTradeTransaction(TradeTransaction trade) {
            trade.setPrice(random.nextDouble() * 9999);
            return trade;
        }
    }
}
